﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = TextBox1.Text;
            string pwd = TextBox2.Text;
            string email = TextBox3.Text;
            string intro = TextBox4.Text;

            string constr = "Data Source = LAPTOP-0NHBA8A3; Initial Catalog =Student_db; Integrated Security = True";
            
            //Data Source = 服务器名称 \ 数据库实例名; Initial Catalog = 数据库名称; Integrate Security = True

            SqlConnection con = new SqlConnection(constr);

            con.Open();

            string str = "insert into StudentInfo(stu_name,password,email,intro) values(@name,@pwd,@email,@intro)";

            SqlParameter[] sql =
            {
                new SqlParameter("@name",name),
                new SqlParameter("@pwd",pwd),
                new SqlParameter("@email",email),
                new SqlParameter("@intro",intro)
            };

            SqlCommand cmd = new SqlCommand(str, con);
            cmd.Parameters.AddRange(sql);

            if (cmd.ExecuteNonQuery()>0)
            {
                Literal1.Text = "注册成功！"+ "  <a href='Login.aspx'>登入>>></a>";
            }
            else
            {
                Literal1.Text = "注册失败！用户名可能重复";
            }
            //SqlCommand cmd=new SqlCommand()
        }
    }
}